package com.example.myanim;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private  boolean flag=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
        //帧动画
        RelativeLayout relativeLayout=findViewById(R.id.anim);
        //获取drwable资源
        final AnimationDrawable anim=(AnimationDrawable)relativeLayout.getBackground();
        relativeLayout.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if(flag){
                    anim.start();
                    flag=false;
                }else{
                    anim.stop();
                    flag=true;
                }
            }
        });

         */
        /*
        //补间动画
        final ImageView imageview =findViewById(R.id.iv);
        imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //通过加载xml动画设置文件来创建一个Animation对象
//                Animation animation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha);//透明度

//                Animation animation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate);//旋转

//                Animation animation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale);//缩放

                Animation animation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate);//平移
                imageview.startAnimation(animation);
            }
        });

         */
        //属性动画
        ValueAnimator valueAnimator=ValueAnimator.ofFloat(0f,1f);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float val=(float)valueAnimator.getAnimatedValue();
                Log.e("属性动画","值="+val);
            }
        });
//        valueAnimator.start();
        TextView text=findViewById(R.id.tv);
        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(text,"alpha",0f,1f);
        objectAnimator.setDuration(3000);
        objectAnimator.start();
        objectAnimator.addListener(new Animator.AnimatorListener() {
            //动画开始
            @Override
            public void onAnimationStart(Animator animator) {

            }
            //动画结束
            @Override
            public void onAnimationEnd(Animator animator) {

            }
            //动画被取消
            @Override
            public void onAnimationCancel(Animator animator) {

            }
            //动画重复执行
            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }
}
